package HomeWork;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StudentNameGenerator {
    public static void main(String[] args) throws Exception {
        String strXingURL="https://hanyu.baidu.com/shici/detail?from=kg1&highlight=&pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&srcid=51369";
        String nvmingXingURL="http://www.haoming8.cn/mingzi/25022.html";
        String nanmingXingURL="http://mingzi.jb51.net/qiming/nanhai/68814860866.html";
        String xingContet=webCarawler(strXingURL);
        String nvmingContet=webCarawler(nvmingXingURL);
        String nanmingContet=webCarawler(nanmingXingURL);
        ArrayList<String> listnan=getdata(nanmingContet,"(..、){4}..");
        ArrayList<String> listnv=getdata(nvmingContet,"(..、){4}..");
        ArrayList<String> listXing= getdata(xingContet,"(.{4})(，|。)");
        ArrayList<String> listXing1=xingData(listXing);
        ArrayList<String> listnan1=mingData(listnan,"男");
        ArrayList<String> listnv1=mingData(listnv,"女");
        ArrayList<String> NanName=goodname(listXing1,listnan1,50);
        ArrayList<String> NvName=goodname(listXing1,listnv1,50);
        List<String> collect = Stream.concat(NanName.stream(),NvName.stream()).collect(Collectors.toList());
        FileWriter fw=new FileWriter("fw.txt");
        for(String s:collect){
            fw.write(s+"\n");
            fw.flush();
        }fw.close();
    }

    private static ArrayList<String> goodname(ArrayList<String> listXing1, ArrayList<String> listnan1, int i) {
        HashSet<String> set=new HashSet<>();
        while(true){
            if(set.size()==i){
                break;
            }
            Collections.shuffle(listnan1);
            Collections.shuffle(listXing1);
            set.add(listXing1.get(0)+listnan1.get(0));
        }
        return new ArrayList<String>(set);
    }

    private static ArrayList<String> mingData(ArrayList<String> listming,String string) {
        Random r=new Random();
        ArrayList<String> ming=new ArrayList<>();
        for(String s:listming){
            String[] split = s.split("、");
            for(String sr:split){
                ming.add(sr+"-"+string+"-"+(r.nextInt(7)+18));
            }

        }
        return ming;
    }
    private static ArrayList<String> xingData(ArrayList<String> listXing) {
        ArrayList<String> xing=new ArrayList<>();
        for(String string:listXing){
            for(int i=0;i<string.length()-1;i++){
                xing.add(string.charAt(i)+"");
            }
        }
        return xing;
    }
    private static ArrayList<String> getdata(String nanmingContet, String regex) {
        ArrayList<String> list=new ArrayList<String>();
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(nanmingContet);
        while(matcher.find()){
            String group = matcher.group();
            if(group.indexOf('e')==-1){
                list.add(group);
            }
        }
        return list;
    }
    private static String webCarawler(String strXingURL) throws Exception {
        StringBuilder sb=new StringBuilder();
        //创建一个url对象
        URL url =new URL(strXingURL);
        //连接上这个网站，确保网络通常
        URLConnection openConnection = url.openConnection();
        InputStream inputStream = openConnection.getInputStream();
        //读取数据
        InputStreamReader isr=new InputStreamReader(inputStream,"utf-8");
        int ch;
        while((ch=isr.read())!=-1){
            sb.append((char)ch);
        }
        isr.close();
        return sb.toString();
    }
}
